package jedis;

import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by wangyg6 on 2017/11/23.
 */
public class jedisDemo {
	/**
	 * jedis链接
	 */
	@Test
	public void demo1() {
		Jedis jedis = null;

		try {
			//设置ip,端口
			jedis = new Jedis("10.128.90.194", 6379);
			jedis.auth("A07L22R07y10qL");
			jedis.connect();
			//选择db
			jedis.select(3);

			jedis.set("name", "xiaoming");
			jedis.setex("key", 1200, "value");

			String name = jedis.get("name");
			String key = jedis.get("key");
			System.out.println(name);
			System.out.println(key);
		} finally {
			if (jedis != null)
				jedis.close();
		}

	}

	/**
	 * JedisPool连接池
	 */
	@Test
	public void demo2() {
		JedisPoolConfig config = new JedisPoolConfig();
		//最大空闲连接数
		config.setMaxIdle(10);
		//最大连接数
		config.setMaxTotal(30);
		//设置cofig,ip,port,超时时间,password,db
		JedisPool jedisPool = new JedisPool(config, "10.128.90.194", 6379, 60, "A07L22R07y10qL", 3);
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.set("TestJedisPool", "jedisPoolTest");
			String result = jedis.get("TestJedisPool");
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (jedis != null)
				jedis.close();
			if(jedisPool!=null)
				jedisPool.close();
		}
	}
}
